﻿#region (c) 2022 Gilles Macabies All right reserved

// Author     : Gilles Macabies
// Solution   : FilterDataGrid
// Projet     : DemoApp.Net7.0
// File       : MainWindow.xaml.cs
// Created    : 04/12/2022
//

#endregion

using FilterDataGrid;
using SharedModelView.ModelView;
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace DemoApp.Net7._0
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

#if DEBUG
            AppDomain.CurrentDomain.FirstChanceException += (source, e) =>
            {
                Debug.WriteLine("FirstChanceException event raised in " +
                                $"{AppDomain.CurrentDomain.FriendlyName}: {e.Exception.Message} {source}");
            };
#endif
            DataContext = new ModelView();

            // if you want to change column order and column names (AutoGenerateColumns="True")
            FilterDataGridAuto.AutoGeneratedColumns += (sender, _) =>
            {
                if (sender is not FilterDataGrid.FilterDataGrid { AutoGenerateColumns: true } datagrid) return;

                // old order of columns
                //  FirstName       0
                //  LastName        1
                //  Manager         2
                //  Salary          3
                //  Age             4
                //  StartDate       5

                // first, change the contents of the headers if necessary
                datagrid.Columns[0].Header = "First name";
                datagrid.Columns[1].Header = "Last name";
                datagrid.Columns[5].Header = "Start Date";

                // hidding "Last name" column
                datagrid.Columns[0].Visibility = Visibility.Hidden;

                // you can sort list columns by an iteration of it
                var list = datagrid.Columns.Select(c => new { ((Binding)((DataGridTextColumn)c).Binding).Path.Path })
                    .ToList();

                //foreach (var item in list.Select((name, id) => new { id, name = name.Path }))
                //    Debug.WriteLine($"{item.name,-10}   {item.id}");

                foreach (var item in list.OrderBy(c => c.Path).Select((name, id) => new { id, name.Path }))
                {
                    var index = list.FindIndex(a => a.Path == item.Path);
                    datagrid.Columns[index].DisplayIndex = item.id;
                }

                // Or directly by column index
                /*
                datagrid.Columns[4].DisplayIndex = 0; // Age          4 => 0
                datagrid.Columns[0].DisplayIndex = 1; // FirstName    0 => 1
                datagrid.Columns[1].DisplayIndex = 2; // LastName     1 => 2
                datagrid.Columns[2].DisplayIndex = 3; // Manager      2 => 3
                datagrid.Columns[3].DisplayIndex = 4; // Salary       3 => 4
                datagrid.Columns[5].DisplayIndex = 5; // StartDate    5 => 5
                */
            };
        }
    }
}